﻿using GetSmartIDEList.Core.Ulities;
using GetSmartIDEList.Lib.Common;
using GetSmartIDEList.Lib.ORM;
using GetSmartIDEList.Models;
using GetSmartIDEList.WebAPI.Models;
using Newtonsoft.Json;
using System.Data;
using System.Net;
using System.Text;

namespace GetSmartIDEList.WebAPI.Common
{
    public static class CurrentListFunction
    {
        private static MySQLHelper mySQL = new MySQLHelper();

        private static string conStr = AppDBConfig.GetConnectionString();

        private static string reposId = AppConfigurtaionServices.Configuration.GetSection("ReposInf").GetSection("reposId").Value.ToString();
        private static string reposUrl = AppConfigurtaionServices.Configuration.GetSection("ReposInf").GetSection("getReposUrl").Value.ToString();

        /// <summary>
        /// 获取左表List
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<resultData> GetLeftRepos()
        {
            string strSQL = SQLXMLHelper.data.GetLeftList;
            DataSet ds = mySQL.ExecuteDataSet(conStr, strSQL);

            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return Enumerable.Empty<resultData>();
            }
            else
            {
                List<resultData> lstLeft = DataEntityConvert<resultData>.DataTableToList(ds.Tables[0]);

                return lstLeft;
            }
        }

        /// <summary>
        /// 获取右表List
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<resultData> GetRightRepos()
        {
            string strSQL = SQLXMLHelper.data.GetRightList;
            DataSet ds = mySQL.ExecuteDataSet(conStr, strSQL);

            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return Enumerable.Empty<resultData>();
            }
            else
            {
                List<resultData> lstRight = DataEntityConvert<resultData>.DataTableToList(ds.Tables[0]);

                return lstRight;
            }
        }

        /// <summary>
        /// 通过IDE名称获取匹配的IDE列表信息
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public static IEnumerable<resultData> GetDetalisByNames(string names)
        {
            //获取组织
            string allRepos = HttpGet.Get(string.Format(reposUrl, reposId), GetAPIAuthor.GetToken());

            if (allRepos != null)
            {

                List<repos> reposLst = JsonConvert.DeserializeObject<List<repos>>(allRepos);

                List<resultData> findRepos = new List<resultData>();

                if (reposLst == null || reposLst.Count == 0)
                {

                    //List<resultData> empty = new List<resultData>();
                    //empty.Add(new resultData() { id = -1, name = "无数据" });

                    return Enumerable.Empty<resultData>();
                }
                else
                {
                    string[] vs = names.Split('$');
                    foreach (string v in vs)
                    {
                        repos find = reposLst.Find(p => p.name == v);

                        if (find != null)
                        {
                            resultData result = new resultData()
                            {
                                id = find.id,
                                name = $"({find.path})<br/>{find.description}"

                            };
                            findRepos.Add(result);
                        }
                    }
                    return findRepos;
                }
            }
            else
            {
                return Enumerable.Empty<resultData>();
            }
        }

    }
}
